package com.wanxi.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.wanxi.dto.ResultDto;
import com.wanxi.dto.UserDto;
import com.wanxi.model.UserModel;
import com.wanxi.service.UserService;
import com.wanxi.service.impl.UserServiceImpl;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/back/user/findAll")
public class UserFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.乱码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
//        2.得到前端的值
        String username = req.getParameter("username");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        String birthday = req.getParameter("birthday");
        String hobby = req.getParameter("hobby");
        String sex = req.getParameter("sex");
        String enable = req.getParameter("enable");
        String age = req.getParameter("age");
        String startCreateTime = req.getParameter("startCreateTime");
        String endCreateTime = req.getParameter("endCreateTime");
        String startAge = req.getParameter("startAge");
        String endAge = req.getParameter("endAge");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String startBirthday = req.getParameter("startBirthday");
        String endBirthday = req.getParameter("endBirthday");

//        3.封装到model
        UserModel model = UserModel.builder()
                .username(username)
                .email(email)
                .sex(sex)
                .age(Tool.strToInt(age))
                .phone(phone)
                .address(address)
                .birthday(birthday)
                .hobby(hobby)
                .enable(Tool.strToInt(enable))
                .startCreateTime(startCreateTime)
                .endCreateTime(endCreateTime)
                .startAge(Tool.strToInt(startAge))
                .endAge(Tool.strToInt(endAge))
                .page(Tool.strToPage(page))
                .limit(Tool.strToLimit(limit))
                .startBirthday(startBirthday)
                .endBirthday(endBirthday)
                .build();

//        4.调用服务逻辑层
        UserService userService = new UserServiceImpl();
        List<UserDto> result = userService.findAll(model);
        int count = userService.count(model);
//        5.将返回的数据响应给前端
//        以后全部以json对象的格式传递给前端{"age":13,"username":"aaaa"}
//        所以，需要将字符串放入对象,然后用统一格式进行输出
// 响应给前端的值不应该将数据库里面所有的值给前端，比如密码
//        DTO响应给前端的对象,model就是前端给后端的对象
//        引入第三方组件：fastjson
//        JSONObject.toJSONString(list);
        resp.getWriter().println(JSONObject.toJSONString(ResultDto.getInstance(count, result)));
    }


}
